home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / type1ops.ps < prev    next >
Text File  |  1996-01-10  |  7KB  |  190 lines

  1. %    Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % type1ops.ps
  16. % Define the Type 1 font opcodes for use by Ghostscript utilities.
  17.  
  18. % Define lenIV (the number of initial random bytes in the encoded outlines).
  19. % This should be zero, but we set it to 4 for compatibility with PostScript.
  20.  
  21. /lenIV 4 def
  22.  
  23. % Define the Type 1 opcodes we care about.
  24.  
  25. /Type1encode 25 dict
  26. /c_hstem 1 def   dup /hstem <01> put
  27. /c_vstem 3 def   dup /vstem <03> put
  28. /c_vmoveto 4 def   dup /vmoveto <04> put
  29. /c_rlineto 5 def   dup /rlineto <05> put
  30. /c_hlineto 6 def   dup /hlineto <06> put
  31. /c_vlineto 7 def   dup /vlineto <07> put
  32. /c_rrcurveto 8 def   dup /rrcurveto <08> put
  33. /c_closepath 9 def   dup /closepath <09> put
  34. /c_callsubr 10 def   /s_callsubr <0a> def   dup /callsubr s_callsubr put
  35. /c_return 11 def   dup /return <0b> put
  36. /c_escape 12 def
  37.   /ce_dotsection 0 def   /s_dotsection <0c06> def   dup /dotsection s_dotsection put
  38.   /ce_vstem3 1 def   /s_vstem3 <0c01> def   dup /vstem3 s_vstem3 put
  39.   /ce_hstem3 2 def   /s_hstem3 <0c02> def   dup /hstem3 s_hstem3 put
  40.   /ce_seac 6 def   /s_seac <0c06> def    dup /seac s_seac put
  41.   /ce_sbw 7 def   /s_sbw <0c07> def   dup /sbw s_sbw put
  42.   /ce_div 12 def   /s_div <0c0c> def   dup /div s_div put
  43.   /ce_callothersubr 16 def   /s_callothersubr <0c10> def   dup /callothersubr s_callothersubr put
  44.   /ce_pop 17 def   /s_pop <0c11> def   dup /pop s_pop put
  45.   /ce_setcurrentpoint 33 def   /s_setcurrentpoint <0c21> def   dup /setcurrentpoint s_setcurrentpoint put
  46. /c_hsbw 13 def   /s_hsbw <0d> def   dup /hsbw s_hsbw put
  47. /c_endchar 14 def   /s_endchar <0e> def   dup /endchar s_endchar put
  48. /c_rmoveto 21 def   dup /rmoveto <15> put
  49. /c_hmoveto 22 def   dup /hmoveto <16> put
  50.   /s_setcurrentpoint_hmoveto s_setcurrentpoint <8b16> concatstrings def
  51. /c_vhcurveto 30 def   dup /vhcurveto <1e> put
  52. /c_hvcurveto 31 def   dup /hvcurveto <1f> put
  53. def
  54.  
  55. % Define the encoding of numbers.
  56. /c_num1 32 def
  57. /c_num2 247 def
  58. /c_num3 251 def
  59. /c_num4 255 def
  60. /min_enc_num1 c_num1 c_num2 sub 1 add 2 idiv def
  61. /max_enc_num1 c_num2 c_num1 sub 1 sub 2 idiv def
  62. /min_enc_num2 max_enc_num1 1 add def
  63. /max_enc_num2 min_enc_num2 c_num3 c_num2 sub 256 mul add 1 sub def
  64. /min_enc_num3 max_enc_num2 neg def
  65. /max_enc_num3 min_enc_num2 neg def
  66.  
  67. % ------ CharString encoding ------ %
  68.  
  69. % For these utilities, a CharString is represented by a sequence of
  70. % integers or names, either in an array or on the stack.
  71. % Integers represent themselves; names are CharString operator names.
  72. % A CharString in an array is called a "charproc"; a CharString on
  73. % the stack is called a "charstack", and is delimited by a mark.
  74. % Individual elements are called "chartokens".
  75.  
  76. % Compute the length of a CharString.
  77. /chartoken_length    % chartoken -> length
  78.  { dup type /nametype eq
  79.     { Type1encode exch get length
  80.     }
  81.     { dup dup -107 ge exch 107 le and
  82.        { pop 1
  83.        }
  84.        { dup -1131 ge exch 1131 le and { 2 } { 5 } ifelse
  85.        }
  86.       ifelse
  87.     }
  88.    ifelse
  89.  } bind def
  90. /charproc_length    % charproc -> length
  91.  { 0 exch { chartoken_length add } forall
  92.  } bind def
  93. /charstack_length    % charstack -> charstack length
  94.  { counttomark 0 exch -1 1 { index chartoken_length add } for
  95.  } bind def
  96.  
  97. % Write a CharString to a file.  Normally this will be a NullEncode filter
  98. % writing on a string of the correct length.
  99. /chartoken_write    % file chartoken ->
  100.  { dup type /nametype eq
  101.     { Type1encode exch get writestring
  102.     }
  103.     { dup dup -107 ge exch 107 le and
  104.        { 139 add
  105.        }
  106.        { dup dup -1131 lt exch 1131 gt or
  107.       { 1 index 255 write
  108.         2 copy -24 bitshift 255 and write
  109.         2 copy -16 bitshift 255 and write
  110.         2 copy -8 bitshift 255 and write
  111.         255 and
  112.       }
  113.       { dup 0 ge { 16#f694 } { neg 16#fa94 } ifelse add
  114.         2 copy -8 bitshift write 255 and
  115.       }
  116.      ifelse
  117.        }
  118.       ifelse write
  119.     }
  120.    ifelse
  121.  } bind def
  122. /charproc_write        % file charproc ->
  123.  { { 1 index exch chartoken_write } forall pop
  124.  } bind def
  125. /charstack_write    % charstack file ->
  126.  { counttomark 1 sub -1 1 { index 1 index exch chartoken_write } for
  127.    cleartomark
  128.  } bind def
  129.  
  130. % Convert a charproc or charstack to a proper encrypted CharString.
  131. /charproc_string    % charproc -> string
  132.  { mark exch aload pop charstack_string
  133.  } bind def
  134. /charstack_string    % charstack -> string
  135.  { charstack_length lenIV add string
  136.    dup dup length lenIV sub lenIV exch getinterval    % skip lenIV
  137.    /NullEncode filter
  138.    exch 1 index counttomark 1 add 2 roll
  139.    charstack_write closefile
  140. %   4330 exch dup .type1encrypt exch pop readonly
  141.  } bind def
  142.  
  143. % ------ CharString decoding ------ %
  144.  
  145. /Type1decode 256 array
  146. dup 0 32 getinterval
  147.   /-0- /hstem /-2- /vstem /vmoveto /rlineto /hlineto /vlineto
  148.   /rrcurveto /closepath /callsubr /return null /hsbw /endchar /-15-
  149.   /-16- /-17- /-18- /-19- /-20- /rmoveto /hmoveto /-23-
  150.   /-24- /-25- /-26- /-27- /-28- /-29- /vhcurveto /hvcurveto
  151. 33 -1 roll astore pop
  152. dup 12
  153.  { dup read pop dup Type1escape exch known
  154.     { Type1escape exch get }
  155.     { =string cvs dup string length copy }
  156.    ifelse
  157.  } put
  158. 32 1 246 { 2 copy dup 139 sub put pop } for
  159. dup 247 { dup read pop 108 add } put
  160. dup 248 { dup read pop 364 add } put
  161. dup 249 { dup read pop 620 add } put
  162. dup 250 { dup read pop 876 add } put
  163. dup 251 { dup read pop 108 add neg } put
  164. dup 252 { dup read pop 364 add neg } put
  165. dup 253 { dup read pop 620 add neg } put
  166. dup 254 { dup read pop 876 add neg } put
  167. dup 255 { 0 4 { 8 bitshift 1 index read pop add } repeat } put
  168. def
  169. /Type1escape 9 dict
  170.   dup ce_dotsection /dotsection put
  171.   dup ce_vstem3 /vstem3 put
  172.   dup ce_hstem3 /hstem3 put
  173.   dup ce_seac /seac put
  174.   dup ce_sbw /sbw put
  175.   dup ce_div /div put
  176.   dup ce_callothersubr /callothersubr put
  177.   dup ce_pop /pop put
  178.   dup ce_setcurrentpoint /setcurrentpoint put
  179. def
  180.  
  181. % Decode a CharString (unencrypted).
  182. /charstack_read        % file -> (NO MARK) charstack
  183.  { { dup read not { pop exit } if
  184.      Type1decode exch get exec exch
  185.    } loop
  186.  } bind def
  187.